<?php
namespace App\V3\Api\Services;

use App\Models\DataFieldPowerAdminModel;
use App\Models\DataFieldPowerListModel;
use  App\V3\Api\Models\AdminModel;


class DataFieldService
{
    public static function getUserFieldPower($data,$parentId)
    {
        $powerList = DataFieldPowerListModel::getAdminPowerList($data);
        $adminPowerList = DataFieldPowerAdminModel::getAdminPowerList($data);
        $permissionList = array_column($adminPowerList, 'field_id');

        $arr = [];

        foreach ($powerList as $k => $v){
            $arr[$k]['id'] = $v['id'];
            $arr[$k]['field_name'] = $v['name'];

            //是否开启;0:否;1:是
            if(in_array($v['id'], $permissionList)) {
                $arr[$k]['is_enable'] = 1;
            } else {
                $arr[$k]['is_enable'] = 0;
            }

        }

        return ['code' => 0, 'msg' => 'success', 'data' => $arr];
    }

    //设置字段权限
    public static function setFieldPermission($data)
    {
        if(!empty($data['field'])){
            $field = rtrim($data['field'],",");
            $field_list = explode(',',$field);

            DataFieldPowerAdminModel::where(['admin_id' => $data['id']])->update(
                ['status' => DataFieldPowerAdminModel::DELETESTATUS]
            );

            foreach ($field_list as $value){
                $parent_data = DataFieldPowerListModel::where('id', $value)->first();

                if($parent_data){

                    DataFieldPowerAdminModel::updateOrCreate([
                        'admin_id' => $data['id'],
                        'field_id' => $value,
                    ],[
                        'admin_id' => $data['id'],
                        'field_id' => $value,
                        'status' => DataFieldPowerAdminModel::USEDSTATUS
                    ]);

                }
            }
        }
        else {
            DataFieldPowerAdminModel::where('admin_id', $data['id'])
                ->update([
                    'status' => 0
                ]);
        }
    }

    public static function distinguishPermission($admin_id,$mark)
    {
        $res = DataFieldPowerAdminModel::select('*')
            ->from('data_field_power_admin as a')
            ->leftJoin('data_field_power_list as l', 'a.field_id', '=', 'l.id')
            ->where([
                'a.admin_id' => $admin_id,
                'a.status' => DataFieldPowerAdminModel::USEDSTATUS,
                'l.status' => DataFieldPowerListModel::USEDSTATUS,
                'l.mark' => $mark])
            ->count();

        if($res){
            return 1;
        }else{
            return 0;
        }
    }

    public static function distinguishPermissionList($admin_id)
    {
        $powerList = DataFieldPowerListModel::select('id','mark','param_list')
            ->where([
                'status' => DataFieldPowerListModel::USEDSTATUS,
            ])
            ->get()->toArray();

        $arr = [];

        foreach ($powerList as $k => $v){
            $res = self::distinguishPermission($admin_id,$v['mark']);
            if($res){
                $param_list = explode(",",$v['param_list']);
                foreach ($param_list as $v){
                    array_push($arr,$v);
                }
            }
            #$arr[$k][$v['mark'].'_display_none'] = $res;
        }

        return $arr;
    }
}
